﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;
using DevExpress.XtraSplashScreen;
using LF3Proj.Properties;
using GPM;

namespace LF3Proj
{
    public partial class FrmLogin : DevExpress.XtraEditors.XtraForm
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern int SetForegroundWindow(IntPtr hWnd);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string strclassName, string strWindowName);

        public bool HasLogin = false;
        public FrmMain MainForm;
        public bool LoginSuccess = false;

        public FrmLogin()
        {
            InitializeComponent();
        }

        private void picMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void picClose_Click(object sender, EventArgs e)
        {
            if (!HasLogin)
                this.Close();
            else
            {
                if (ExMessageBox.Confirm("提示信息：确定要关闭程序吗？", AppSetter.SoftName) == DialogResult.No)
                    return;
                if (null != MainForm)
                {
                    MainForm.Dispose();
                    MainForm.Close();
                }
                this.Close();
            }
        }

        private void closemenu_Click(object sender, EventArgs e)
        {
            if (!HasLogin)
                this.Close();
            else
            {
                if (ExMessageBox.Confirm("提示信息：确定要关闭程序吗？", AppSetter.SoftName) == DialogResult.No)
                    return;
                if (null != MainForm)
                {
                    MainForm.Dispose();
                    MainForm.Close();
                }
                this.Close();
            }
        }

        /// <summary>
        /// 判断鼠标是否按下
        /// </summary>
        private Boolean _isDown = false;
        /// <summary>
        /// 原来的鼠标点
        /// </summary>
        private Point _oldPoint;
        /// <summary>
        /// 原来窗口点
        /// </summary>
        private Point _oldForm;

        private void FrmLogin_MouseDown(object sender, MouseEventArgs e)
        {
            _isDown = true;
            _oldPoint = new Point();
            _oldPoint = e.Location;
            _oldForm = this.Location;
        }

        private void FrmLogin_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDown)
            {
                _oldForm.Offset(e.X - _oldPoint.X, e.Y - _oldPoint.Y);
                this.Location = _oldForm;
            }
        }

        private void FrmLogin_MouseUp(object sender, MouseEventArgs e)
        {
            _isDown = false;
        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtUserID.Text))
            {
                ExMessageBox.Show("提示信息：帐号不能为空", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtPwd.Text))
            {
                ExMessageBox.Show("提示信息：密码不能为空", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.LoginSuccess = false;
            //if (null != MainForm)
            //{
            //    MainForm.Dispose();
            //    MainForm.Close();
            //}
            //MainForm = null;
            //HasLogin = false;
            try
            {
                if (CheckInput())
                {
                    
                    UserLoginController controller = new UserLoginController();
                    User_Info user = controller.Login(txtUserID.Text, txtPwd.Text);
                    if (null != user)
                    {
                        AppSetter.CurrentUser = user;
                        //SplashScreenManager.ShowForm(typeof(SplashScreenProgess));
                        //HasLogin = true;
                        this.LoginSuccess = true;
                        if (this.checkRemember.Checked)
                        {
                            AppSetter.SetValue("UserID", txtUserID.Text);
                            AppSetter.SetValue("Password", txtPwd.Text);
                        }
                        else
                        {
                            AppSetter.SetValue("Password", "");
                        }
                        if (this.checkAutoLogin.Checked)
                            AppSetter.SetValue("AutoLogin", "1");
                        else
                            AppSetter.SetValue("AutoLogin", "");

                        this.Close();

                        
                    }
                    else
                    {
                        ExMessageBox.Show("提示信息：登录失败，用户名密码错误", AppSetter.SoftName);
                        this.txtUserID.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                this.txtUserID.Focus();
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {

        }

        private void FrmLogin_Shown(object sender, EventArgs e)
        {
            this.txtUserID.Text = AppSetter.GetConfigString("UserID");
            this.txtPwd.Text = AppSetter.GetConfigString("Password");
            if (string.IsNullOrEmpty(this.txtPwd.Text))
                this.checkRemember.Checked = false;
            else
                this.checkRemember.Checked = true;

            this.txtUserID.Focus();
            if (string.IsNullOrEmpty(AppSetter.GetConfigString("AutoLogin")))
                this.checkAutoLogin.Checked = false;
            else
            {
                this.checkAutoLogin.Checked = true;
                btnLogin_Click(this, new EventArgs());
            }
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLogin.PerformClick();
            }
        }

        private void picConfig_Click(object sender, EventArgs e)
        { 
        }

        private void picConfig_MouseHover(object sender, EventArgs e)
        {
            this.picConfig.Image = Resources.set_hover;
        }

        private void picConfig_MouseLeave(object sender, EventArgs e)
        {
            this.picConfig.Image = Resources.set;
        }

        private void picMin_MouseHover(object sender, EventArgs e)
        {
            this.picMin.Image = Resources.min_hover;
        }

        private void picMin_MouseLeave(object sender, EventArgs e)
        {
            this.picMin.Image = Resources.min ;
        }

        private void picClose_MouseHover(object sender, EventArgs e)
        {
            this.picClose.Image = Resources.close_hover;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            this.picClose.Image = Resources.close;
        }
    }
}